package com.itheima.test4;

import java.util.Random;

public class MyThread extends Thread {

    static double m1;
    static double m2;
    static double m3;

    static {
        Random r = new Random();
        m1 = r.nextDouble(100);
        m2 = r.nextDouble(100 - m1);
        m3 = 100 - m1 - m2;
    }

    static int count = 3;

    @Override
    public void run() {
        synchronized (MyThread.class) {
            if (count <= 0) {
                //抢完了
                System.out.println(Thread.currentThread().getName() + "没抢到");
            } else if (count == 1) {
                System.out.println(Thread.currentThread().getName() + "抢到了" + m1 + "元");
            } else if (count == 2) {
                System.out.println(Thread.currentThread().getName() + "抢到了" + m2 + "元");
            } else if (count == 3) {
                System.out.println(Thread.currentThread().getName() + "抢到了" + m3 + "元");
            }
            count--;
            //到这里的都是抢到了红包的人，全部等待
            try {
                MyThread.class.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
